
package com.xpgk.mvc.statistic.employeeRank;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bouncycastle.crypto.tls.MACAlgorithm;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bloseWeight.DelWeiTarget;
import com.xpgk.mvc.statistic.dailyNursing.DailyNursingStatisticSQL;
import com.xpgk.mvc.statistic.loseWeightList.LoseWeightListSQL;
import com.xpgk.mvc.statistic.spropagandaTraining.SpropagandaTrainingSQL;
import com.xpgk.tools.ToolDateTime;


@Service(name = SemployeeRankService.serviceName)
public class SemployeeRankService extends BaseService{

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(SemployeeRankService.class);
	public static final String serviceName = "semployeeRankService";
	
	
	/*
	 * 员工数据查询
	 */
	
	public List<Record> querySemployee(String organIds,String startdate,String enddate){
		
		HashMap<String, Object> param = new HashMap<String,Object>();
		param.put("organIds", organIds);
		List<Record> result = find(SemployeeRankSQL.semployeerank_querysemployee, param);
		
		//新增顾客情况
		for(Record r : result){
			List<Record> find = Db.find(getSqlMy(SemployeeRankSQL.semployeerank_addcustomerbyemployee), r.getStr("employeeids"),startdate,enddate);
			r.set("advisorynumber",find.size());
			int headcount=0;
			int recommendcomein=0;
			int disseminatecomein=0;
			int herselfcomein=0;
			int otherways=0;
			int coursenumber=0;
			float planloseweight=0;
			for(Record findd : find){
				if(findd.getStr("gkfl").equals("607f10b77f4d4b908104e990f366a334")){
					if(findd.getFloat("onlyplanloseweight")!=null){
						planloseweight = planloseweight+(float)findd.getFloat("onlyplanloseweight");
					}
					headcount++;
					if(findd.getStr("jdfs").equals("7baa3f3fc25d4caa924f963584e5af21")){
						recommendcomein++;
					}else if(findd.getStr("jdfs").equals("214e103ff60040468395995891cd9ee7")){
						disseminatecomein++;
					}else if(findd.getStr("jdfs").equals("7728a6bfa5c044a9bd69d99b2d7e7d94")){
						herselfcomein++;
					}else{
						otherways++;
					}
					if(findd.getStr("onlycoursenumber")!=null){
						coursenumber = coursenumber+Integer.parseInt(findd.getStr("onlycoursenumber"));
					}
				}
			}
			r.set("recommendcomein", recommendcomein);
			r.set("disseminatecomein", disseminatecomein);
			r.set("herselfcomein", herselfcomein);
			r.set("otherways", otherways);
			r.set("headcount", headcount);
			DecimalFormat df = new DecimalFormat("######0.00"); 
			r.set("turnoverratio", df.format((double)headcount/(double)find.size()*100.0));
			r.set("coursenumber", coursenumber);
			r.set("planloseweight", (float)(Math.round(planloseweight*100))/100);
		};
		
		//3到店情况
		for(Record record : result){
			//3.1计划到店次数
//			System.out.println(record.getStr("employeeids"));
			//3.1.1开始日期-当期计划次数
			float startplantime= 0;
			List<Record> startTimeFind = Db.find(getSqlMy(SemployeeRankSQL.semployeerank_mindateplantime),startdate,record.getStr("employeeids"),startdate,startdate,enddate);
			for(Record r :startTimeFind){
				float shouldnumberofdays = Float.parseFloat(r.get("shouldnumberofdays").toString());
				float plantime = Float.parseFloat(r.get("plantime").toString());
				float numberofdays = Float.parseFloat(r.get("numberofdays").toString());
				startplantime = startplantime + plantime/shouldnumberofdays*numberofdays;
			}
			//3.1.2结束日期-当期计划次数
			float endplantime=0;
			List<Record> endTimeFind = Db.find(getSqlMy(SemployeeRankSQL.semployeerank_maxdateplantime),enddate,record.getStr("employeeids"),enddate,enddate,startdate);
			for(Record r : endTimeFind){
				float shouldnumberofdays = Float.parseFloat(r.get("shouldnumberofdays").toString());
				float plantime = Float.parseFloat(r.get("plantime").toString());
				float numberofdays = Float.parseFloat(r.get("numberofdays").toString());
				endplantime = endplantime + plantime/shouldnumberofdays*numberofdays;
				System.out.println("++++++++++++++++++"+endplantime);
			}
			//3.1.3开始-结束日期之间的完整当期计划次数
			//ps：两边的边角已经算好了 就算中间的然后三者相加 就可以放到result里面了
		
			
		}
		
		
		return result;
	}
}
